package com.mkyong.common;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import com.mkyong.utils.JSFUtils;

@ManagedBean
@SessionScoped
public class HelloBean implements Serializable {
	private static final long serialVersionUID = 1L;
	
	@ManagedProperty(value="#{message}")
	private MessageBean messageBean;
	
	private String name ;
	
	public void setMessageBean(MessageBean messageBean) {
		this.messageBean = messageBean;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void attrListener(ActionEvent event) {
		name = (String) event.getComponent().getAttributes().get("username");
	}
	
	public String getSayWelcome() {
		if("".equals(name) || null == name) {
			return "";
		} else {
			messageBean.setSayWelcome("Good afternoon!");
			String fromCountry = JSFUtils.getRequestParam("country");
//			return "Ajax message : Welcome " + name;
			return messageBean.getSayWelcome() + " " + name + " from " + fromCountry;
		}
	}

}
